<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="../lib/ext/resources/css/ext-all-neptune.css"/>
    <link rel="stylesheet" type="text/css" href="../lib/ckeditor/sample.css" />
    <script type="text/javascript" src="../lib/ext/ext-all.js"></script>
    <script type="text/javascript" src="../lib/gf/common_grid.js"></script>
    <script type="text/javascript" src="../lib/ext/plugins/datetime/UX_TimePickerField.js"></script>
    <script type="text/javascript" src="../lib/ext/plugins/datetime/UX_DateTimePicker.js"></script>
    <script type="text/javascript" src="../lib/ext/plugins/datetime/UX_DateTimeField.js"></script>
    <script type="text/javascript" src="../lib/ext/plugins/datetime/UX_DateTimeMenu.js"></script>
    <script type="text/javascript" src="../lib/gf/common_alert.js"></script>


</head>
<script type="text/javascript">
    Ext.Loader.setConfig({enabled: true});
    var dataForm;
    var data = {};
    Ext.onReady(function () {
        //保存数据
        function doAdd(){
            var params = dataForm.form.getValues();
            params.parent_id = parent_id;
            if(!dataForm.form.isValid()){
                Ext.Msg.alert('系统提示','请修正指出错误，检查输入是否正确!');
                return;
            }

            var _url = '/res/add';
            if(params && params._id ){
                _url = '/res/edit';
            }
            Ext.Ajax.request({
                url: _url,
                params : params,
                method : 'POST',
                success: function(response, opts) {
                    var data = eval("("+response.responseText+")");
                    if(data.flag == "success") {
                        reloadLeftFrame();
                        alert_msg_refresh_close_win('业务处理成功！');
                    }else {
                        alert_msg('业务处理失败！'+data.msg);
                    }


                },
                failure: function(response, opts) {
                    alert('业务处理失败');
                }
            });
        }

        //刷新左边树
        function reloadLeftFrame(){
            if(parent.parent.document.getElementById("leftFrame") != null)
                parent.parent.document.getElementById("leftFrame").contentWindow.location.reload();
        }
        //关闭窗口
        function doClose(){
            parent.closeWin();
        }
        var tbar = [
            {pressed: true,text: '保  存',handler: doAdd,icon : '../img/book.png'},
            {pressed: true,text: "关  闭",handler: doClose,icon : '../img/close.png'}
        ];
        dataForm = new Ext.FormPanel({
            layout : 'fit',
            items: [
                {
                    xtype: 'form',
                    defaultType: 'textfield',
                    items: [
                        {
                            name : '_id',
                            hidden : true
                        },
                        {
                            xtype:'hiddenfield',
                            name:'parent_id',
                            value:parent_id
                        },
                        {
                            fieldLabel: '资源编码（英文）',
                            labelWidth:120,
                            name : 'res_code',
                            allowBlank: false
                        },{
                            fieldLabel: '资源名称',
                            labelWidth:120,
                            name : 'res_name',
                            allowBlank: false
                        },{
                            fieldLabel: '资源URL',
                            labelWidth:120,
                            width : 500,
                            name : 'url',
                            allowBlank: true
                        },{
                            xtype      : 'fieldcontainer',
                            fieldLabel : '资源类型',
                            labelWidth:120,
                            defaultType: 'radiofield',     //定义为radiofield
                            defaults: {
                                flex: 1
                            },
                            layout: 'hbox',
                            items: [
                                {
                                    boxLabel  : '菜单',
                                    name      : 'type',
                                    inputValue: 'menu',
                                    id        : 'type1',
                                    checked   : true
                                }, {
                                    boxLabel  : '操作',
                                    name      : 'type',
                                    inputValue: 'operate',
                                    id        : 'type2'
                                }
                            ]
                        },
                        {
                            xtype      : 'fieldcontainer',
                            fieldLabel : 'HTTP_METHOD',
                            defaultType: 'radiofield',     //定义为radiofield
                            labelWidth:120,
                            defaults: {
                                flex: 1
                            },
                            layout: 'hbox',
                            items: [
                                {
                                    boxLabel  : 'ALL',
                                    name      : 'http_method',
                                    inputValue: 'ALL',
                                    id        : 'ALL',
                                    checked   : true
                                }, {
                                    boxLabel  : 'GET',
                                    name      : 'http_method',
                                    inputValue: 'GET',
                                    id        : 'GET'
                                }, {
                                    boxLabel  : 'POST',
                                    name      : 'http_method',
                                    inputValue: 'POST',
                                    id        : 'POST'
                                }
                            ]
                        },
                        {
                            xtype      : 'fieldcontainer',
                            fieldLabel : '状态',
                            labelWidth:120,
                            defaultType: 'radiofield',     //定义为radiofield
                            defaults: {
                                flex: 1
                            },
                            layout: 'hbox',
                            items: [
                                {
                                    boxLabel  : '有效',
                                    name      : 'status',
                                    inputValue: '1',
                                    id        : 'radio1',
                                    checked   : true
                                }, {
                                    boxLabel  : '无效',
                                    name      : 'status',
                                    inputValue: '0',
                                    id        : 'radio2'
                                }
                            ]
                        },
                        {
                            fieldLabel:'排序',
                            name:'order',
                            xtype:'numberfield',
                            labelWidth:120,
                            value:1

                        }
                    ],
                    tbar : tbar
                }
            ]
        });
        var viewport = new Ext.Viewport({
            layout: 'fit',
            renderTo: "mypanel",
            items: [dataForm]
        });



        //获取url中的参数，如果传入了参数_id那么进行修改；否则，添加。
        var _urlparams = Ext.urlDecode(document.URL.split("?")[document.URL.split("?").length - 1]);
        var parent_id = _urlparams.parent_id;
        if(_urlparams && _urlparams._id){
            Ext.Ajax.request({
                url: '/res/detail',
                params : {'_id' : _urlparams._id},
                method : 'get',
                success: function(response, opts) {
                    data =  Ext.decode(response.responseText);
                    init_form();
                },
                failure: function(response, opts) {
                    alert('加载失败......');
                    init_form();
                }
            });
        }

        function init_form(){
            if(!data){
                return ;
            }
            for(var i in data){
                if(dataForm.form.findField(i)){
                    dataForm.form.findField(i).setValue(data[i]);
                }
            }
        }
    });
</script>
<body>
<div id="mypanel" style="height:1024px;width:100%"></div>
</body>
</html>